package com.west_traveler.find_page_fragment.find_live_provinceradio_frg;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.test.mock.MockApplication;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_live_page.entity_live_second_page.RadioInfo;
import com.west_traveler.entity.entity_live_page.entity_live_second_page.SecondFirstData;
import com.west_traveler.inteface.LiveHttpService;
import com.west_traveler.recyclerview_adapter.NormalLiveSecondRecyclerAdapter;
import com.west_traveler.ximalayafm.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*
* 省市广播界面frg
* @author Wang_JunJie
* @date 2016/12/9
*
*/
public class ProvinceRadioFrg extends Fragment {


    private int pageNum=1;
    private int provinceCode;
    private boolean loading;
    private NormalLiveSecondRecyclerAdapter adapter;
    public ProvinceRadioFrg(int provinceCode) {
        this.provinceCode=provinceCode;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_province_radio_frg, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_provinceradio);
        //数据源
        final LiveHttpService service = ((BaseApplication) getActivity().getApplication()).getLiveHttpService();
        service.getSecondLocalData(pageNum,20,provinceCode,2).enqueue(new Callback<SecondFirstData>() {


            @Override
            public void onResponse(Call<SecondFirstData> call, Response<SecondFirstData> response) {
                //数据源
                List<RadioInfo> datas = response.body().getResult();
                //适配器
                adapter = new NormalLiveSecondRecyclerAdapter(getContext());
                adapter.addDatas(datas);
                //绑定适配器
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<SecondFirstData> call, Throwable t) {

            }
        });

        //添加监听（分页）
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int itemCount = linearLayoutManager.getItemCount();
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                if(!loading && itemCount==lastVisibleItemPosition+1 && itemCount>=20){
                    loading=true;
                    service.getSecondLocalData(++pageNum,20,provinceCode,2).enqueue(new Callback<SecondFirstData>() {
                        @Override
                        public void onResponse(Call<SecondFirstData> call, Response<SecondFirstData> response) {
                            List<RadioInfo> datas = response.body().getResult();
                            adapter.addDatas(datas);
                            loading=false;
                            if(datas.size()!=20){
                                loading=true;
                            }
                        }

                        @Override
                        public void onFailure(Call<SecondFirstData> call, Throwable t) {

                        }
                    });
                }

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });


    }
}
